package com.ruoyi.recycle.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.recycle.domain.*;
import com.ruoyi.recycle.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/otherSetting")
@RequiredArgsConstructor
@Api(tags = "其他设置")
@Slf4j
public class OtherSettingController extends BaseController {
    private final FactoryAnnouncementService announcementService;
    private final CompanyVideoService companyVideoService;
    private final RankingListService rankingListService;
    private final CompanyPictureService companyPictureService;
    private final CompanyIntroductionService companyIntroductionService;

    @GetMapping("/announcementDetail")
    @ApiOperation("查看工厂公告详情")
    @ApiImplicitParam(name = "id", value = "工厂公告id", type = "Long")
    public R<FactoryAnnouncement> announcementDetail(@RequestParam Long id) {
        return R.ok(announcementService.getById(id));

    }

    @PostMapping("/setUpAnnouncement")
    @ApiOperation("编辑工厂公告")
    public R<Boolean> setUpAnnouncement(@RequestBody FactoryAnnouncement announcement) {
        try {
            announcementService.saveOrUpdate(announcement);
            return R.ok(true,"保存公告成功");
        } catch (Exception e) {
            return R.fail(false, "设置公告失败");
        }
    }

    @GetMapping("/rankingListDetail")
    @ApiOperation("获取排行榜详情")
    @ApiImplicitParam(name = "id", value = "排行榜id", type = "Long")
    public R<RankingList> rankingListDetail(@RequestParam Long id) {
        return R.ok(rankingListService.getById(id));
    }

    @ApiOperation("编辑排行榜")
    @PostMapping("/setUpRankingList")
    public R<Boolean> setUpRankingList(@RequestBody RankingList saveList) {
        try {
            rankingListService.saveOrUpdate(saveList);
            return R.ok(true, "保存排行榜信息成功");
        } catch (Exception e) {
            log.error("保存排行榜信息失败,原因:{}", e.getMessage());
            return R.fail(false, "保存排行榜信息失败");
        }

    }

    @ApiOperation("获取公司视频列表")
    @GetMapping("/queryCompanyVideo")
    public R<List<CompanyVideo>> queryCompanyVideo() {
        return R.ok(companyVideoService.list());
    }


    @ApiOperation("编辑公司视频")
    @PostMapping("/setUpCompanyVideo")
    @Transactional(rollbackFor = Exception.class)
    public R<Boolean> setUpCompanyVideo(@RequestBody List<CompanyVideo> companyVideos) {
        companyVideoService.deleteAll();
        companyVideoService.saveBatch(companyVideos);
        return R.ok(true, "保存公司视频成功");


    }

    @ApiOperation("获取公司图片")
    @GetMapping("/getCompanyPicture")
    public R<CompanyPicture> getCompanyPicture() {
        return R.ok(companyPictureService.getById(1L));

    }

    @PostMapping("/editCompanyPicture")
    @ApiOperation("编辑公司图片")
    public R<Boolean> editCompanyPicture(@RequestBody CompanyPicture picture) {
        try {
            companyPictureService.saveOrUpdate(picture);
            return R.ok(true, "保存公司图片成功");

        } catch (Exception e) {
            log.error("保存公司图片失败,原因:{}", e.getMessage());
            return R.fail(false, "保存公司图片失败");
        }
    }

    @ApiOperation("获取公司简介")
    @GetMapping("/getCompanyIntroduction")
    public R<CompanyIntroduction> getCompanyIntroduction() {
        return R.ok(companyIntroductionService.getById(1L));

    }

    @PostMapping("/editCompanyIntroduction")
    @ApiOperation("编辑公司简介")
    public R<Boolean> editCompanyIntroduction(@RequestBody CompanyIntroduction introduction) {
        try {
            companyIntroductionService.saveOrUpdate(introduction);
            return R.ok(true, "保存公司简介成功");

        } catch (Exception e) {
            log.error("保存公司介绍失败,原因:{}", e.getMessage());
            return R.fail(false, "保存公司介绍失败");
        }
    }
}
